package com.wy.jersey;

import java.net.URL;
import java.util.Iterator;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jst.j2ee.web.componentcore.util.WebArtifactEdit;
import org.eclipse.jst.j2ee.webapplication.Servlet;
import org.eclipse.jst.j2ee.webapplication.ServletMapping;
import org.eclipse.jst.j2ee.webapplication.ServletType;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.jst.j2ee.webapplication.WebapplicationFactory;

import com.wy.base.AbstractWizard;

public class RestWebserviceWizard extends AbstractWizard {

	// private RestWebservicePage page;

	/**
	 * Constructor for RESTfulWebserviceWizard.
	 */
	public RestWebserviceWizard() {
		super();
		setNeedsProgressMonitor(true);
	}

	/**
	 * Adding the page to the wizard.
	 */
	@Override
	public void addPages() {
		// page = new RestWebservicePage(selection);
		// addPage(page);
		setPage(new RestWebservicePage(this.getSelection()));
		addPage(getPage());
	}

	@Override
	public boolean performFinish() {
		return super.performFinish("/templates/jersey.template");
	}

	@SuppressWarnings({ "unchecked", "unused" })
	public void addServlet(String containerName) throws Exception {
		loadProperties();
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(containerName));
		IContainer container = (IContainer) resource;
		IJavaProject project = (IJavaProject) container.getProject().getNature("org.eclipse.jdt.core.javanature");
		IProject ip = (IProject) project.getCorrespondingResource();
		Servlet servlet = null;
		EList<Servlet> servlets;
		boolean exists;
		WebArtifactEdit artifact = WebArtifactEdit.getWebArtifactEditForWrite(ip);
		WebApp webApp = artifact.getWebApp();
		exists = false;
		servlets = webApp.getServlets();
		Iterator<Servlet> iterator = servlets.iterator();
		while (iterator.hasNext()) {
			Servlet ser = iterator.next();
			// if (ser.getServletClass().getJavaName().equals(this.jerseyClass)) {
			if (ser.getServletClass().getJavaName().equals(this.getJerseyClass())) {
				exists = true;
				break;
			}
		}
		if (!exists) {
			servlet = WebapplicationFactory.eINSTANCE.createServlet();
			servlet.setServletName("ServletAdaptor");
			ServletType type = WebapplicationFactory.eINSTANCE.createServletType();
			// type.setClassName(jerseyClass);
			type.setClassName(super.getJerseyClass());
			servlet.setWebType(type);
			servlet.setLoadOnStartup(Integer.valueOf(1));
			webApp.getServlets().add(servlet);
			ServletMapping mapping = WebapplicationFactory.eINSTANCE.createServletMapping();
			mapping.setServlet(servlet);
			mapping.setUrlPattern("/" + "rs" + "/*");
			webApp.getServletMappings().add(mapping);
		}
		artifact.saveIfNecessary(null);
		artifact.dispose();
		IFolder dir = getWebInfLibDir(ip);
		if (dir.exists()) {
			// int len = this.jerseyJars.length;
			int len = this.getJerseyJars().length;
			for (int i = 0; i < len; i++) {
				// String jar = jerseyJars[i];
				String jar = this.getJerseyJars()[i];
				if (!dir.getFile(jar).exists()) {
					try {
						int split = jar.lastIndexOf('-');
						String folder = jar.substring(0, split) + ".jar/";
						// dir.getFile(jar).create(new URL(this.downloadSite + jar).openStream(), false,
						// null);
						dir.getFile(jar).create(new URL(this.getDownloadSite() + jar).openStream(), false, null);
					} catch (Exception e) {
						System.err.println(e.getMessage());
					}
				}
			}
		}
	}
}